const util = require('../../utils/util.js')
const Net = require('../../Net.js')
const md5 = require('../../utils/md5')
Page({
  data: {
    freshStatus: 'more',
    showRefresh: false,
    id: 0,
    top: Number(getApp().globalData.statusBarHeight + 150),
    tops: Number(getApp().globalData.statusBarHeight + 98),
    currentTab: 0,
    bgShow: false,
    scroll_state: 0,
    imageurl2: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
    imageurl3: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
    swiperCur: 0,
    OtherHide: true,
    classify_id: 0,
    NetWorkStatus: true,
    onList: [{
      id: 0,
      type: '综合',
      onClickStatus: false,
      onClickImg: '',
      statusType: 'new'
    }, {
      id: 1,
      type: '销量',
      onClickStatus: false,
      onClickImg: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
      statusType: 'new'
    }, {
      id: 2,
      type: '佣金',
      onClickStatus: false,
      onClickImg: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
      statusType: 'new'
    }, {
      id: 3,
      type: '价格',
      onClickStatus: false,
      onClickImg: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
      statusType: 'new'
    }],
    showWhite: 0,
    indicator_dots:true
  },
  onHidmodal() {
    this.setData({
      showFl: false
    })
  },
  //跳转搜索页
  goLogin: util.throttle(function (e) {
    wx.navigateTo({
      url: '/pages/Login/Index/index',
    })
  }, 1000),
  onBingNews() {
    wx.navigateTo({
      url: '/pages/onNewPages/onMessagePage/onMessagePage',
    })
  },
  //滑动页面
  onPageScroll: function (e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  //banner点击
  onBannerUrl(e) {
    var typeName = e.currentTarget.dataset.typename,
      id = e.currentTarget.dataset.id,
      type = e.currentTarget.dataset.type
    console.log(e.currentTarget.dataset.type)
    if (type == 2) {
      wx.navigateTo({
        url: '/pages/onNewPages/onDownLoadFile/onDownLoadFile?url=' + typeName,
      })
    } else {
      if (typeName == 'product') {
        Net.link('v1/pddGoods/' + id, {
            id: id
          },
          function (res) {
            wx.setStorageSync('detail', res.data.data)
            wx.navigateTo({
              url: '/pages/onNewPages/Shop/Detail/Detail',
            })
          }, 'GET',
          function (err) {})
      }
      if (typeName == 'buyvip') {
        var userPuls = wx.getStorageSync('member')
        if (userPuls.payment) {
          wx.navigateTo({
            url: '/pages/onNewPages/onPlusCip/onPlusCip',
          })
        } else {
          wx.navigateTo({
            url: '/pages/onNewPages/onUserOutPlus/onUserOutPlus',
          })
        }
      }
    }
  },
  textfunction(e) {
    console.log('加载完毕', e.currentTarget.id)
    var _this = this,
      list = _this.data.list
    list[e.currentTarget.id].loadingStatus = true
    _this.setData({
      list: list
    })
  },
  // 触摸开始
  touchStart(e) {
    const classify_id = this.data.classify_id
    if (classify_id == 0) {
      this.setData({
        startY: e.changedTouches[0].pageY,
        freshStatus: 'more'
      })
    } else {
      wx.stopPullDownRefresh({})
    }
  },
  //邀请码
  onInvitation() {
    var _this = this,
      token = wx.getStorageSync('token')
    if (!token) {
      wx.navigateTo({
        url: '/pages/Login/Index/index'
      })
      return false
    }
    wx.navigateTo({
      url: '/pages/Login/getCode/getCode',
    })

  },
  // 触摸移动
  touchMove(e) {
    let endY = e.changedTouches[0].pageY;
    let startY = this.data.startY;
    let dis = endY - startY;
    var _this = this
    // 判断是否下拉
    if (dis <= 0) {
      return;
    }
    let offsetTop = e.currentTarget.offsetTop;
    if (dis > 20) {
      _this.setData({
        showRefresh: true
      }, () => {
        if (dis > 50) {
          _this.setData({
            freshStatus: 'end'
          })
        } else {
          _this.setData({
            freshStatus: 'more'
          })
        }
      })
    } else {
      _this.setData({
        showRefresh: false
      })
    }
  },
  // 触摸结束
  touchEnd(e) {
    if (this.data.freshStatus == 'end') {
      // 延迟 500 毫秒，显示 “刷新中”，防止请求速度过快不显示
      setTimeout(() => {
        this.setData({
          freshStatus: 'fresh'
        })
      }, 200);
      setTimeout(() => {
        this.setData({
          showRefresh: false
        })
        wx.stopPullDownRefresh()
      }, 2000);
    } else {
      this.setData({
        showRefresh: false
      })
    }
  },
  //反弹
  touchEnds(e) {
    wx.stopPullDownRefresh()
  },
  swiperChanges: function (e) {
    console.log(e)
    this.setData({
      swiperCur: e.detail.current
    })
  },
  switchNav(event) {
    var cur = event.currentTarget.dataset.current;
    var id = event.currentTarget.dataset.id;
    var sort = this.data.sort;
    //每个tab选项宽度占1/5
    var singleNavWidth = (this.data.windowWidth) / 7;
    //tab选项居中                            
    this.setData({
      swiperCur: 0,
      navScrollLeft: (cur - 2.3) * singleNavWidth,
      classify_name: '',
      classify_id: cur,
      now_page: 1
    })
    const data = {
      classify_id: cur,
      classify_name: '',
      now_page: 1
    }
    this.data.list = ''
    this.goods(data)
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur,
        id: id,
        cid: cur,
      })
    }
    //首页第一个默认显示
    if (this.data.currentTab == 0) {
      this.setData({
        OtherHide: true
      })
    } else {
      this.setData({
        OtherHide: false,
        Classify: this.data.navDatas[cur].child
      })
    }
  },
  ChangeTab(e) {
    var cur = e.currentTarget.dataset.current + 1;
    var singleNavWidth = (this.data.windowWidth) / 7;
    //tab选项居中                            
    this.setData({
      swiperCur: 0,
      navScrollLeft: (cur - 2.3) * singleNavWidth,
      classify_name: '',
      classify_id: cur,
      now_page: 1,
      currentTab: cur,
      id: cur,
      cid: cur,
      OtherHide: false
    })
    const data = {
      classify_id: cur,
      classify_name: '',
      now_page: 1,
    }
    this.data.list = ''
    this.goods(data)
    //首页第一个默认显示
    if (cur == 0) {
      this.setData({
        OtherHide: true,
        showFl: false,
      })
    } else {
      this.setData({
        showFl: false,
        Classify: this.data.navDatas[cur].child
      })
    }
  },
  //分类
  shows() {
    this.setData({
      showFl: !this.data.showFl
    })
  },
  //扫码
  ScanCode() {
    wx.scanCode({
      success: res => {
        console.log(res)
      }
    })
  },
  //跳转搜索页
  Search: util.throttle(function (e) {
    wx.navigateTo({
      url: '/pages/onNewPages/Shop/Search/Search?search_txt=' + this.data.Index.search_txt,
    })
  }, 1000),
  //图标数据
  appIndex(data) {
    var _this = this
    Net.link('v1/appIndex', data, function (res) {
      _this.setData({
        Index: res.data.data,
        NetWorkStatus: true
      })
    }, 'GET', function (err) {
      wx.clearStorage()
      const data = {
        classify_id: this.data.classify_id,
        now_page: 1,
        classify_name: 1
      }
      _this.appIndex('')
      _this.tbClassify('')
      _this.goods(data)
      console.log('缓存导致接口错误，将要清除所有数据')
    })
  },
  //首页分类数据
  tbClassify(data) {
    Net.link('v1/pddClassify', data, function (res) {
      wx.stopPullDownRefresh()
      let navs = {
        cid: 0,
        name: '推荐'
      };
      let nav = res.data.data
      nav.unshift(navs)
      Net.data({
        navDatas: nav,
        NetWorkStatus: true
      })
    }, 'GET', function (err) {
      wx.clearStorage()
      const data = {
        classify_id: this.data.classify_id,
        now_page: 1,
        classify_name: 1
      }
      _this.appIndex('')
      _this.tbClassify('')
      _this.goods(data)
      console.log('缓存导致接口错误，将要清除所有数据')
    })
  },
  //首页数据
  goods(data) {
    Net.toast('加载中', 'loading')
    Net.link('v1/pddGoods', data, function (res) {
      Net.hid();
      const list = res.data.data.list
      for (let i in list) {
        var title = list[i].tao_title.split('')
        var shop_title = list[i].shop_title.split('')
        list[i].loadingStatus = false
        if (title.length > 20) {
          list[i].tao_title = list[i].tao_title.substring(0, 18) + '...'
          if (shop_title.length > 6) {
            list[i].shop_title = list[i].shop_title.substring(0, 6) + '...'
          }
        }
      }
      Net.data({
        list: Net.data('list') ? Net.data('list').concat(list) : list,
        now_page: res.data.data.page
      })
      setTimeout(() => {
        Net.data({
          showWhite: 1,
        })
        setTimeout(() => {
          Net.data({
            showWhite: 2,
          })
        }, 500);
      }, 1000);
      if (res.data.data.list.length == 10) {
        Net.data({
          scroll_state: 1
        })
      } else {
        wx.stopPullDownRefresh()
        Net.data({
          scroll_state: 0
        })
      }
    }, 'GET', function (err) {
      Net.data({
        scroll_state: 2
      })
    })
  },
  onLoad: function (options) {
    var _this = this
    setTimeout(() => {
      Net.data({
        bgShow: true,
      })
    }, 1000)

    //获取元素信息
    var query = wx.createSelectorQuery();
    //选择id
    query.select('.header').boundingClientRect(function (rect) {
      Net.data({
        height: rect.height + 15 + 'px',
        modelTop: rect.height + 13 + 'px',
      })
    }).exec();
    //data数据
    const data = {
      classify_id: _this.data.classify_id,
      now_page: 1,
      classify_name: 1
    }
    //获取元素宽高
    wx.getSystemInfo({
      success: (res) => {
        Net.data({
          windowWidth: res.windowWidth
        })
      },
    })
    _this.appIndex('')
    _this.tbClassify('')
    _this.goods(data)
    wx.getNetworkType({
      complete: (res) => {
        console.log(res)
        if (res.networkType == 'none') {
          Net.data({
            NetWorkStatus: false
          })
        }
      },
    })
  },
  onReady: function () {
    this.toast = this.selectComponent('#toast')
  },
  showToast(e) {
    var _this = this,
      list = _this.data.list,
      DS = e.currentTarget.id
    wx.setStorageSync('detail', list[DS])
    wx.navigateTo({
      url: '/pages/onNewPages/Shop/Detail/Detail',
    })
  },
  onShow: function () {
    this.setData({
      token: wx.getStorageSync("token")
    })
    wx.getNetworkType({
      complete: (res) => {
        console.log(res)
        if (res.networkType == 'none') {
          this.setData({
            NetWorkStatus: false
          })
        }
      },
    })
  },
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {
    var _this = this
    wx.showNavigationBarLoading()
    _this.appIndex('')
    _this.tbClassify('')
    _this.goods()
  },
  onAgainLoadingTap() {
    var _this = this
    const data = {
      classify_id: this.data.classify_id,
      now_page: 1,
      classify_name: 1
    }
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    _this.appIndex('')
    _this.tbClassify('')
    _this.goods(data)
  },
  onReachBottom: function () {
    var _this = this
    let page = this.data.now_page;
    page++;
    let data = {
      classify_id: this.data.classify_id,
      now_page: page,
      classify_name: 1
    }
    _this.goods(data)
  }
})